﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace SIN.Client.Controller
{
    class addonsListController
    {
        string mAddons = "";
        string mAddonsInfo = "";

        public void checkAddonsList()
        {
            string aPath = Program.addonsListModel.dateIdentifierPath();
            if (System.IO.File.Exists(aPath))
            {
                this.parseAddonsList();
            }
            else
            {
                Program.mainForm.startUpdate();                
            }        
        }
        
        public void parseAddonsList() 
        {
            string aAddons = "";
            string aInfo = "";

            string aPath = Program.addonsListModel.filePath();
            Program.mainForm.setIdle(true);
            Program.mainForm.setStatus("Start addons list file parsing");

            HtmlDocument aUI9Source = new HtmlDocument();
            aUI9Source.Load(aPath, Encoding.UTF8);

            if (aUI9Source.DocumentNode != null)
            {
                HtmlNode aAddonFull = aUI9Source.GetElementbyId("tabs-1");
                if (aAddonFull != null)
                {
                    HtmlNode aAddonTables = aAddonFull.FirstChild;
                    if (aAddonTables != null)
                    {
                        foreach (HtmlNode aAddonRow in aAddonTables.ChildNodes)
                        {
                            if (aAddonRow != null)
                            {
                                foreach (HtmlNode aAddonDate in aAddonRow.Descendants("span"))
                                {
                                    string aUpdate = "";
                                    aUpdate = aAddonDate.InnerHtml;
                                    aUpdate = aUpdate.Replace("\n","\0");
                                    aUpdate = aUpdate.Replace("<b>", "\0");
                                    string[] aInfoList = aUpdate.Split('\0');
                                    int i = 0;
                                    foreach (string aElem in aInfoList)
                                    {
                                        if (i == 0) if (aElem != "") aAddons += aElem+"\n" ;
                                        i++;
                                    }
                                }
                                foreach (HtmlNode aAddonTable in aAddonRow.Descendants("table"))
                                {
                                    if (aAddonTable != null)
                                    {
                                        string aText = aAddonTable.InnerText.ToString();
                                        aText = aText.Replace("\t", string.Empty);
                                        aText = aText.Replace("\n", "\0");
                                        string[] aAddon = aText.Split('\0');
                                        int i = 0;
                                        string aAddonName = "";
                                        string aAddonDesk = "";
                                        foreach (string aElem in aAddon){
                                            if (i == 2) aAddonName = aElem;
                                            if (i == 5) aAddonDesk = aElem;
                                            if (i == 6) aInfo += aAddonName + "\t" + aAddonDesk + "\n";
                                            i++;
                                        }
                                    }
                                }

                                mAddons = aAddons;
                                mAddonsInfo = aInfo;
                            }
                        }
                        Program.mainForm.setStatus("Addons list file parsed");
                        Program.mainForm.setIdle(false);
                        Program.mainForm.setAddonsList();
                    }
                }
            }
            else 
            {
                Program.mainForm.setStatus("Can't parse empty addons list file");
            }
        }

        public string getAddonsList()
        {
            return mAddons;
        }

        public void getAddonInfo(int aAddonIndex)
        {   
            string [] aAddonsInfoRows = mAddonsInfo.Split('\n');            
            Program.mainForm.setAddonDescription(aAddonsInfoRows[aAddonIndex]);
        }
    }
}
